<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2017/6/5
 * Time: 上午9:38
 */

namespace app\apitemp\controller;

use app\common\controller\ActionLogs;
use app\common\model\Agent;
use app\common\model\Merchant;
use think\Config;

class PrivateApi
{
    public function api()
    {
        try {
            $postData = file_get_contents('php://input');
            if ($postData) {
                $encode = mb_detect_encoding($postData, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5'));
                $postData = mb_convert_encoding($postData, "UTF-8", $encode);

                if (is_json($postData)) {
                    $param1 = json_decode($postData, true);
                    $query_type = $param1['method'];
                    $param = json_decode($postData, true);
                    $check = $this->checkparam(array_keys($param), $query_type);
                    if ($check['code'] == "0") {
                        unset($param1['sign']);
                        if ($param['appid'] == "common_agent_appid") {
                            $sign = $this->getsign(uni_array($param1), "a1a31daa8c3d915d2c7b9f27a6f7712a","MD5");

                            if ($sign == $param['sign']) {
                                $data = $this->api_query($param);

                            } else {
                                $data['return_code'] = "SUCCESS";
                                $data['result_code'] = "FAIL";
                                $data['result_msg'] = "签名错误";
                                $data['return_msg'] = "签名错误";
                            }
                        }else{
                            $data['return_code'] = "SUCCESS";
                            $data['result_code'] = "FAIL";
                            $data['result_msg'] = "非法操作";
                            $data['return_msg'] = "非法操作" ;
                        }
                    } else {
                        $data['return_code'] = "SUCCESS";
                        $data['result_code'] = "FAIL";
                        $data['result_msg'] = $check['message'];
                        $data['return_msg'] = $check['message'];
                    }
                } else {
                    $data['return_code'] = "FAIL";
                    $data['result_code'] = "FAIL";
                    $data['result_msg'] = "数据格式错误";
                    $data['return_msg'] = "请传递正确的数据格式";
                }
            } else {
                $data['return_code'] = "FAIL";
                $data['result_code'] = "FAIL";
                $data['result_msg'] = "数据格式错误";
                $data['return_msg'] = "请传递正确的数据格式";
            }
        } catch (\Exception $e) {
            $data['return_code'] = "FAIL";
            $data['result_code'] = "FAIL";
            $data['result_msg'] = "系统错误";
            $data['return_msg'] = "系统错误" . $e->getMessage();
        }
        return json_encode($data);


    }

    private function checkparam($param, $type)
    {
        switch ($type) {

            case "login":
                $sdata = array('appid', 'username', 'password', 'device_id', 'version', 'timestamp', 'source', 'signaturenonce');
                break;

        }

        $arr = array_diff($sdata, $param);
        $arr2 = implode(',', $arr);

        if (!empty($arr2)) {
            $arrs['code'] = "FAIL";
            $arrs['message'] = "缺少参数" . $arr2;
        } else {
            $arrs['code'] = "0";
            $arrs['message'] = "SUCCESS";

        }
        return $arrs;
    }

    public function param_query($param)
    {
        $type = $param['method'];
        switch ($type) {
            case "cardbalancerec":
                if ($param['pay_cate'] == "Cash" || $param['pay_cate'] == "Electronics") {
                    if ($param['pay_cate'] == "Electronics") {
                        if ($param['out_trade_no'] !== "") {
                            $arrs['code'] = "SUCCESS";
                            $arrs['message'] = "SUCCESS";

                        } else {
                            $arrs['code'] = "FAIL";
                            $arrs['message'] = "out_trade_no 参数不能为空";
                        }
                    } else {
                        $arrs['code'] = "SUCCESS";
                        $arrs['message'] = "SUCCESS";
                    }

                } else {
                    $arrs['code'] = "FAIL";
                    $arrs['message'] = "pay_cate 参数不正确";
                }
                break;
        }
        return $arrs;
    }

    public function api_query($array)
    {
        try {

            $type = $array['method'];
            switch ($type) {

                //用户登录接口
                case "login":
                    $data = action('api/login/privatelogin', array($array));
                    break;
            }
        } catch (\Exception $e) {
            $data['return_code'] = "SUCCESS";
            $data['result_code'] = "FAIL";
            $data['result_msg'] = "系统错误";
            $data['return_msg'] = "系统错误" . $e->getMessage();
        }
        $data['version'] = "2.0.0";
        $data['timestamp'] = time();
        $data['signaturenonce'] = MD5(rand(1000000, 9999999));
        $data['sign'] = getsign(uni_array($data), "MD5", $array['appid']);
        $logs['type'] = "apilog";
        $logs['up'] = $array;
        $logs['down'] = $data;
        $ActionLogs = new ActionLogs();
        $ActionLogs->writelog("osapi", "clientlog", "log", "client:", $logs);
        return $data;
    }

    //判断appid是否与merchantid匹配
    private function checkappid($appid, $merchantid)
    {
        $map['sign_appid'] = $appid;
        $Agent = new Agent();
        $Agents = $Agent->where($map)->field('id')->select();
        foreach ($Agents as $k => $v) {
            $agentid[] = $v['id'];
        }
        $sid = implode(',', $agentid);
        $mab['agent'] = array('in', $sid);
        $mab['id'] = $merchantid;
        $Merchant = new Merchant();
        $mach = $Merchant->where($mab)->find();
        if ($mach) {
            return "SUCCESS";
        } else {
            return "FAIL";
        }


    }

    private function getsign($arrdata, $appkey, $method = "sha1")
    {
        try {
            if (!function_exists($method)) return false;
            ksort($arrdata);
            $paramstring = "";
            foreach ($arrdata as $key => $value) {
                if (isset($value) && !empty($value)) {
                    $str[] = $key . "=" . $value;
                }
            }
            $paramstring = join("&", $str);
            $Sign = $method($paramstring . "&key=" . $appkey);

            return $Sign;
        } catch (\Exception $e) {
            return $e;
        }
    }


}